<?php
class JSDropDown {

	 //Member varible Declaration
	 var $db_con;
	 var $rs_row;
 	 var $ERR_MSG; 
	 var $regionArrayResult;
	 
	 // Declare Array Variable;
	 var $cityArray;
	 var $regionArray;

     // Declare a variable for Assigning the Current Form Name
	 var $CurrectForm;
	 
	     
	function JSDropDown($dbConfig){
     //Purpose: Constructer to Handle Database And Value initialize 
              $this->db_con=mysqli_connect($dbConfig['server'],$dbConfig['username'],$dbConfig['password'],$dbConfig['database']) or die(mysqli_error($this->db_con));
		      
			  // Declare the variables as Array
   	          $this->regionArray=array();   
	          $this->cityArray = array();			  
	}
	
	function close_JSDropDown_database(){
	//Purpose: Close the Database Connectivity
	    mysqli_close($this->db_con);
	}
	
	
	function DataFetch(){ 
	//Purpose: Fetch the Data From 2 Different Table and store it in array
	
	// Suggestions: If you have changed the Table Names Please Change it in the sql;
	    $citySql="select * from citymaster";   
		$cityResult=mysqli_query($this->db_con,$citySql);
		$IDx=0;$IDy=0;$IDz=0;
		$this->regionArrayResult=array();
		while ($cityRow = mysqli_fetch_array($cityResult,MYSQLI_BOTH)){
			for($i=0;$i<mysqli_num_fields($cityResult);$i++){
					$this->cityArray[$IDx][$i] = $cityRow[$i];
			}	
			$regionSql="select * from region WHERE city_id='".$cityRow["city_id"]."'";		   
			$regionResult=mysqli_query($this->db_con,$regionSql);
			$IDz=mysqli_num_rows($regionResult);
			while($regionRow = mysqli_fetch_array($regionResult)){
			        for($i=0;$i<mysqli_num_fields($regionResult);$i++){
					    $this->regionArray[$IDy][$i] = $regionRow[$i];
					}
					$this->regionArray[$IDy][$i]=$cityRow[1]; 		
					$IDy+=1;         
			}// Region While Loop Ends here;
		   $IDx+=1; 
		} // City Region Loop End 
	}// DataFetch Function Ends here
	
	function createfrmObject($frmObjName){ 
	//Purpose: Create the Javascript Dynamically
	    $this->CurrectForm= $frmObjName;
	    $DocElement = "document.".$this->CurrectForm.".region";
      echo "<script type=\"text/javascript\" language=\"javascript\">";
		  echo "<!--\n";
      echo "\rfunction changeRegion(obj){\r";
      for($i=0;$i<count($this->cityArray);$i++){
         echo "\rif(obj.value == '".$this->cityArray[$i][0]."'){\r";
         echo "\r\t/* Value for the Region Drop Down */\r";
         echo "\tdocument.$this->CurrectForm.region.options.length = 0;\r";
         echo "\t$DocElement.options[$DocElement.options.length] = new Option('--Select--','--Select--');\r";
         for($j=0;$j<Count($this->regionArray);$j++){
             if ( $this->cityArray[$i][0] == $this->regionArray[$j][1]){
                echo "\t$DocElement.options[$DocElement.options.length] = new Option('".$this->regionArray[$j][2]."','".$this->regionArray[$j][0]."');\r";
             }
         }
         echo "\tdocument.$this->CurrectForm.region.disabled = false;\n\r";
         echo "\treturn true;\r";
         echo "}\r";
                      
      }
   		echo "}\r";
	 	  echo "-->\r";
	    echo "</script>";	
	    $this->CreateSelect();
   }
    
   function CreateSelect(){
        echo "<select name=\"city\" size=\"1\" onChange='changeRegion(this)'>\r";
		    echo "<option value='--Select--'>--Select--</option>\r";
   		  for($i=0;$i<count($this->cityArray);$i++){
		         echo "<option value='".$this->cityArray[$i][0]."'>".$this->cityArray[$i][1]."</option>\r";
  	    }
		    echo "</select> &nbsp; Area&nbsp;";
        echo "<select name=\"region\" disabled>\r";
        echo "<option value='--Select--'>--Select--</option>\r";		 
	      echo "</select>\r";
   }
	
} // Class JSDropDown Ends Here

?>

